<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="作品封面" align="center" prop="work_pic" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="发布时间" align="center" prop="time" />
      <el-table-column label="展现" align="center" prop="exhibit" />
      <el-table-column label=" 阅读" align="center" prop="read" />
      <el-table-column label="评论" align="center" prop="comment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>


          <el-form-item label="作品封面" prop="work_pic">
            <el-input v-model="form.work_pic" placeholder="请输入封面地址" />
          </el-form-item>


          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" />
          </el-form-item>

          <el-form-item label="发布时间" prop="time">
            <el-input v-model="form.time" placeholder="请输入发布时间" />
          </el-form-item>



          <el-form-item label="展现" prop="exhibit">
            <el-input v-model="form.exhibit" placeholder="请输入展现量" />
          </el-form-item>


          <el-form-item label="阅读" prop="read">
            <el-input v-model="form.read" placeholder="请输入阅读量" />
          </el-form-item>


          <el-form-item label="评论" prop="comment">
            <el-input v-model="form.comment" placeholder="请输入公告标题" />
          </el-form-item>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getWorkApi, editWorkApi, delWorkApi } from "@/api/all";
import moment from 'moment';
export default {
  name: "Notice",

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        avatar: undefined,
        createBy: undefined,
        status: undefined,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
      pickerOptions: {
        disabledDate(time) {
          // 禁用未来时间
          return time.getTime() > Date.now();
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      console.log(this.$store.getters.userId, '刷新完就没了');
      getWorkApi({
        userId: this.$store.getters.userId
      }).then(res => {
        this.noticeList = res.data
        if (this.noticeList.length > 0) {
          this.noticeList.forEach(item => {
            item.bank_time_copy = moment(item.bank_time).format("YYYY-MM-DD HH:mm:ss");
          })
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        work_pic: undefined,
        title: undefined,
        time: undefined,
        exhibit: undefined,
        read: undefined,
        comment: undefined,
        user_id: undefined,
        id: undefined,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true;
      this.title = "修改数据";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {

          if (this.form.id != undefined) {
            editWorkApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.user_id = this.$store.getters.userId
            editWorkApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const delId = row.id
      let that = this
      this.$modal.confirm('是否确认删除').then(function () {
        that.delNotice(delId)
      })
    },
    // 删除
    delNotice(id) {
      delWorkApi({
        id
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      })
    }
  }
};
</script>
